/**
 * Created by zhongb on 2017/4/11.
 */
var $ = require('jquery');
require('easyui');
var $alert = require('libs/alert');
$(function () {
    //获取url参数
    var orderId = $.getUrlParam("id");
    var status = {
        new: '新建',
        committed: '已提交',
        picking: '拣货中',
        pick: '已拣货',
        packing: '装箱中',
        pack: '已装箱',
        weight: '已称重',
        waitOut: '待出库',
        outroom: '已出库',
        cancelled: '已取消'
    };
    fillData(orderId);
    tableInit(orderId);
    function fillData(orderId) {
        $.get('api/v1/p/mms/fbaTransportOrder/load',{id: orderId},function (res) {
            if(res.code == 200){
                var data = res.data;
                var $firstTarget = $("#baseInfo").find("span");
                //元素赋值
                $firstTarget.eq(0).text(data.orderNo);
                $firstTarget.eq(1).text(data.entName);
                $firstTarget.eq(2).text(data.warehouseName);
                $firstTarget.eq(3).text(data.fbaWarehouseName);
                $firstTarget.eq(4).text(data.deliveryWay);
                $firstTarget.eq(5).text(data.submitDt);
                $firstTarget.eq(6).text(data.outroomDt);
                $firstTarget.eq(7).text(status[data.status]);
                var cfs = $('#commodityFiles');
                var bfs = $('#boxFiles');
                $.each(data.goodsFiles,function (i,o) {
                    cfs.append('<a href="'+o.filePath+'" download>'+o.fileRealName+'</a>');
                });
                $.each(data.boxFiles,function (i,o) {
                    bfs.append('<a href="'+o.filePath+'" download>'+o.fileRealName+'</a>');
                });
            }else{
                $alert(res.message);
            }
        });
    }
    function tableInit(orderId) {
        $("#commodityTable").datagrid({
            url:'api/v1/p/mms/fbaTransportOrder/goodslist',
            border: false,
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 50, 100],
            singleSelect: true,
            rownumbers: true,
            emptyMsg: '暂无数据',
            queryParams: {
                id: orderId
            },
            columns:[[
                {
                    field : 'goodsCode',
                    title : '商品编码',
                    width : '25%'
                },{
                    field : 'chName',
                    title : '中文名称',
                    width : '25%'
                },{
                    field : 'nums',
                    title : '数量',
                    width : '25%'
                },{
                    field : 'fnSku',
                    title : 'FNSKU',
                    width : '25%'
                }
            ]],
            onClickRow: function (rowIndex, rowData) {
                $(this).datagrid('unselectRow', rowIndex);
            },
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            }
        });
    }
    $("#boxTable").datagrid({
            url:'api/v1/p/mms/fbaTransportOrder/boxlist',
            border: false,
            pagination: true,
            pageSize: 10,
            pageList: [10, 20, 50, 100],
            singleSelect: true,
            rownumbers: true,
            emptyMsg: '暂无数据',
            queryParams: {
                id: orderId
            },
            columns:[[
                {
                    field : 'boxNo',
                    title : '箱号',
                    width : '13%'
                },{
                    field : 'deliveryWayNo',
                    title : '运单号',
                    width : '13%'
                },{
                    field : 'factLength',
                    title : '包裹尺寸(CM)',
                    width : '15%',
                    formatter:function (value,row,index) {
                        return value+'×'+row.factWidth+'×'+row.factHeight;
                    }
                },{
                    field : 'factWeight',
                    title : '包裹重量(Kg)',
                    width : '12%'
                },{
                    field : 'skuNums',
                    title : 'SKU数量',
                    width : '10%'
                },{
                    field : 'nums',
                    title : '商品数量',
                    width : '10%'
                },{
                    field : 'barCode',
                    title : '箱单条码',
                    width : '17%'
                },{
                    field : 'opt',
                    title : '操作',
                    width : '10%'
                }
            ]],
            onClickRow: function (rowIndex, rowData) {
                $(this).datagrid('unselectRow', rowIndex);
            },
            loadFilter:function(data){
                if(data.code == '200'){
                    return data.data;
                }else{
                    return [];
                }
            }
        });
});
